package com.chuanyang.driverh.mainhome.fragment;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainstart.adapter.IncvocePriceAdapterLeader;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.InvoceOrderResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.pick.AddressPickTask;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import cn.qqtheme.framework.entity.City;
import cn.qqtheme.framework.entity.County;
import cn.qqtheme.framework.entity.Province;
import cn.qqtheme.framework.picker.DatePicker;
import okhttp3.Call;

public class BidHallFragment extends BaseListFragment {
    private IncvocePriceAdapterLeader listAdapter;

    private DriverInfoData driverinfo;
    private View title_bar;
    private LinearLayout ll_time,ll_addr_start,ll_addr_end,ll_credit;
    private TextView tv_time_show,tv_addr_start_show,tv_addr_end_show,tv_credit_show;
    private String date;
    private String addrStart;
    private String addrEnd;
    private String credit;
    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new IncvocePriceAdapterLeader(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.common_part_bidhall, null);
        ll_time=(LinearLayout) title_bar.findViewById(R.id.ll_time);
        ll_addr_start=(LinearLayout) title_bar.findViewById(R.id.ll_addr_start);
        ll_addr_end=(LinearLayout) title_bar.findViewById(R.id.ll_addr_end);
        ll_credit=(LinearLayout) title_bar.findViewById(R.id.ll_credit);
        tv_time_show=(TextView) title_bar.findViewById(R.id.tv_time_show);
        tv_addr_start_show=(TextView) title_bar.findViewById(R.id.tv_addr_start_show);
        tv_addr_end_show=(TextView) title_bar.findViewById(R.id.tv_addr_end_show);
        tv_credit_show=(TextView) title_bar.findViewById(R.id.tv_credit_show);

        if (root.getChildCount()>0)root.removeAllViews();
        root.addView(title_bar,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData() {
        driverinfo= BaseApplication.getInstance().getDriverInfo();
        getData();
    }

    @Override
    protected void initListener() {
        ll_time.setOnClickListener(this);
        ll_addr_start.setOnClickListener(this);
        ll_addr_end.setOnClickListener(this);
        ll_credit.setOnClickListener(this);
    }


public void resetChoose(){
    date="";
    addrStart="";
    addrEnd="";
    credit="";
    tv_time_show.setText("请选择");
    tv_addr_start_show.setText("请选择");
    tv_addr_end_show.setText("请选择");
    tv_credit_show.setText("请选择");
    onRefresh();
}

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_time:
               onYearMonthDayPicker();
                break;
            case R.id.ll_addr_start:
                cityPick(tv_addr_start_show);
                break;
            case R.id.ll_addr_end:
                cityPick(tv_addr_end_show);
                break;
            case R.id.ll_credit:
                break;
        }
    }

    public void cityPick(final  TextView tv) {
        AddressPickTask task = new AddressPickTask(getActivity());
        task.setHideCounty(true);
        task.setCallback(new AddressPickTask.Callback() {
            @Override
            public void onAddressInitFailed() {
                PrintUtil.toast(getActivity(),"数据初始化失败");
            }
            @Override
            public void onAddressPicked(Province province, City city, County county) {
                String cityName=city.getAreaName();
                tv.setText(cityName);
                if(tv.getId()== R.id.tv_addr_start_show){
                    addrStart=cityName;
                }else{
                    addrEnd=cityName;
                }
                onRefresh();
            }
        });
        task.execute("山东", "日照");
    }


    public void onYearMonthDayPicker() {
        final DatePicker picker = new DatePicker(getActivity());
        Calendar c=Calendar.getInstance();
        int year= c.get(Calendar.YEAR);
        int month= c.get(Calendar.MONTH)+1;
        int day= c.get(Calendar.DAY_OF_MONTH);

        c.setTimeInMillis(System.currentTimeMillis()+7*24*60*60*1000);
        int yearEnd= c.get(Calendar.YEAR);
        int monthEnd= c.get(Calendar.MONTH)+1;
        int dayEnd= c.get(Calendar.DAY_OF_MONTH);

        picker.setTopPadding(6);
        picker.setRangeStart(year, month, day);
        picker.setRangeEnd(yearEnd, monthEnd,dayEnd);
        picker.setSelectedItem(year, month, day);


        picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                date=year + "-" + month + "-" + day;
                tv_time_show.setText( month + "-" + day);
                onRefresh();
            }
        });
        picker.setOnWheelListener(new DatePicker.OnWheelListener() {
            @Override
            public void onYearWheeled(int index, String year) {
                picker.setTitleText(picker.getSelectedMonth() + "-" + picker.getSelectedDay());
            }

            @Override
            public void onMonthWheeled(int index, String month) {
                picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
            }

            @Override
            public void onDayWheeled(int index, String day) {
                picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
            }
        });
        picker.show();
    }



    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        if (driverinfo.getDriver() != null && driverinfo.getDriver().getDriverType() != null) {
            if (driverinfo.getDriver().getDriverType() == 2){//车
                params.put("transPortWay", "1");
            } else {//3 船
                params.put("transPortWay", "2");
            }
        }
        if (!StringUtil.isBlank(date)) {
            params.put("order_startime", date);
        }
        if (!StringUtil.isBlank(addrStart)) {
            params.put("startAddr",addrStart);
        }
        if (!StringUtil.isBlank(addrEnd)) {
            params.put("targetAddr", addrEnd);
        }
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        params.put("driverId", SharedPrefUtil.getUserId());
        MyHttpUtil.bidOrder(getActivity(),params, new MyCallback<InvoceOrderResult>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(InvoceOrderResult result, int id) {
                if (result.getCode() == ConstUtil.OK && result.getData()!=null && result.getData().getData()!=null &&result.getData().getData().size()>0) {
                    try {
                        setDataResult(result.getData().getData());
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                }else{
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==0) {
            onRefresh();
        }
    }
    @Override
    public void onGetMore() {
        getData();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }
}
